package health.lm.com.shop.model;

import org.json.JSONObject;

import health.lm.com.component_base.pay.PaymentEntity;
import health.lm.com.data.App;
import health.lm.com.data.HttpCST;
import health.lm.com.data.entity.OrderSubmitResultEntity;
import health.lm.com.network.EasyHttp;
import health.lm.com.network.callback.SimpleCallBack;
import health.lm.com.network.utils.MyApi;

/**
 * Created by Android Studio .
 * 作者：zhong
 * 日期：2018/12/13
 * 邮箱：15206394364@163.com
 * 介绍：购物车
 * 修订：====================
 */
public class ShoppingCartModel {
    private static ShoppingCartModel shoppingCartModel;

    private ShoppingCartModel() {
    }

    public static ShoppingCartModel getInstance() {
        if (null == shoppingCartModel) shoppingCartModel = new ShoppingCartModel();
        return shoppingCartModel;
    }


    /**
     * 删除购物车
     *
     * @param cart_id   购物车ID
     * @param pCallBack observer
     */
    public void DeleteShoppingCart(String cart_id, SimpleCallBack<Object> pCallBack) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.CART_ID, cart_id);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1003, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }

    /**
     * 增加减少购物车商品数量
     *
     * @param cart_id   购物车ID
     * @param pCallBack pCallBack
     */
    public void ShoppingCartNumChange(String cart_id, int type, SimpleCallBack<Object> pCallBack) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST.CART_ID, cart_id);
            jsonObject.put(HttpCST.TYPE, type);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1004, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }

    /**
     * 加入购物车
     *
     * @param pCallBack observer
     */
    public void addToShopCart(String goods_id, String spec_id, String num, SimpleCallBack<Object> pCallBack) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("goods_id", goods_id);
            jsonObject.put("spec_id", spec_id);
            jsonObject.put("num", num);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1001, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }

    /**
     * 立即购买 确认订单接口
     *
     * @param jsonObject 数据源
     * @param pCallBack  pCallBack
     */
    public void BuyNowOrderSubmit(JSONObject jsonObject, SimpleCallBack<OrderSubmitResultEntity> pCallBack) {
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1008, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }

    /**
     * 糖果兑换 立即购买 确认订单接口
     *
     * @param jsonObject 数据源
     * @param pCallBack  pCallBack
     */
    public void ExchangeBuyNowOrderSubmit(JSONObject jsonObject, SimpleCallBack<Object> pCallBack) {
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1025, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }

    /**
     * 订单团购提交订单
     *
     * @param jsonObject 数据源
     * @param pCallBack  pCallBack
     */
    public void GroupBuyNowOrderSubmit(JSONObject jsonObject, SimpleCallBack<OrderSubmitResultEntity> pCallBack) {
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1020, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }

    /**
     * 订单评价
     *
     * @param orderId   订单ID
     * @param star      星级
     * @param content   内容
     * @param pCallBack pCallBack
     */
    public void OrderEvaluation(String orderId, String star, String content, SimpleCallBack<Object> pCallBack) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put(HttpCST._id, orderId);
            jsonObject.put("star", star);
            jsonObject.put("content", content);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1014, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }

    /**
     * 获取支付参数
     *
     * @param order_id  订单ID
     * @param pay_type  支付方式
     * @param pCallBack pCallBack
     */
    public void getReadyPay(String order_id, int pay_type, SimpleCallBack<PaymentEntity> pCallBack) {
        JSONObject jsonObject = new JSONObject();
        String json = "";
        try {
            jsonObject.put(HttpCST.ACCESS_TOKEN, App.getModel().getAccess_token());
            jsonObject.put("order_id", order_id);
            jsonObject.put("pay_type", pay_type);
            json = MyApi.getInstance().generateMessage(HttpCST.MODULE_ORDER, HttpCST.INTFC_1016, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EasyHttp.post("/").upJson(json).execute(pCallBack);
    }
}
